/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmInscription.java
 *
 * Created on 2011-05-18, 18:27:56
 */

package pckGestionHoraire;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import pckCompSup.JTextFieldMaxLength;
import pckFiles.Encodage;
import pckFiles.IniFile;
import pckSQL.SQL_Connection;
import pckSQL.SQL_Etudiant;

/**
 *
 * @author Portable-Maison
 */
public class FrmInscription extends javax.swing.JFrame {

     SQL_Connection gaConnect;
     SQL_Etudiant gaEtudiant;

    /** Creates new form FrmInscription */
    public FrmInscription() {
        initComponents();
        positionnerFenetre();
        chargerInformation();
        creeComposant();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlAccueil = new javax.swing.JPanel();
        pnlInfo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txfNom = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txfPrenom = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txfMatricule = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txfMotPasse = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        cbxProgramme = new javax.swing.JComboBox();
        pnlAction = new javax.swing.JPanel();
        btnSuivant = new javax.swing.JButton();
        btnFermer = new javax.swing.JButton();
        pnlInformation = new javax.swing.JPanel();
        pnlRapport = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaAffichage = new javax.swing.JTextArea();
        pnlAction1 = new javax.swing.JPanel();
        btnPrecedent = new javax.swing.JButton();
        btnValider = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Inscription d'un étudiant");
        setMinimumSize(new java.awt.Dimension(400, 160));
        getContentPane().setLayout(new java.awt.CardLayout(2, 2));

        pnlAccueil.setMaximumSize(new java.awt.Dimension(700, 225));
        pnlAccueil.setMinimumSize(new java.awt.Dimension(650, 170));
        pnlAccueil.setPreferredSize(new java.awt.Dimension(650, 180));
        pnlAccueil.setLayout(new java.awt.GridBagLayout());

        pnlInfo.setMaximumSize(new java.awt.Dimension(700, 160));
        pnlInfo.setMinimumSize(new java.awt.Dimension(700, 150));
        pnlInfo.setPreferredSize(new java.awt.Dimension(700, 150));
        pnlInfo.setRequestFocusEnabled(false);
        pnlInfo.setLayout(new java.awt.GridLayout(5, 2, 10, 10));

        jLabel1.setDisplayedMnemonic('N');
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setLabelFor(txfNom);
        jLabel1.setText("Nom de l'étudiant :");
        pnlInfo.add(jLabel1);

        txfNom.setName("Nom"); // NOI18N
        txfNom.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ValiderInformation(evt);
            }
        });
        pnlInfo.add(txfNom);

        jLabel2.setDisplayedMnemonic('p');
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setLabelFor(txfPrenom);
        jLabel2.setText("Prenom de l'étudiant :");
        pnlInfo.add(jLabel2);

        txfPrenom.setName("Prenom"); // NOI18N
        txfPrenom.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ValiderInformation(evt);
            }
        });
        pnlInfo.add(txfPrenom);

        jLabel3.setDisplayedMnemonic('m');
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setLabelFor(txfMatricule);
        jLabel3.setText("Matricule :");
        pnlInfo.add(jLabel3);

        txfMatricule.setName("Matricule"); // NOI18N
        txfMatricule.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ValiderInformation(evt);
            }
        });
        pnlInfo.add(txfMatricule);

        jLabel4.setDisplayedMnemonic('p');
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setLabelFor(txfMotPasse);
        jLabel4.setText("Mot de passe :");
        pnlInfo.add(jLabel4);

        txfMotPasse.setName("MotPasse"); // NOI18N
        txfMotPasse.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ValiderInformation(evt);
            }
        });
        pnlInfo.add(txfMotPasse);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Programme :");
        pnlInfo.add(jLabel5);

        cbxProgramme.setName("Programme"); // NOI18N
        cbxProgramme.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ValiderInformation(evt);
            }
        });
        pnlInfo.add(cbxProgramme);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.ipady = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        pnlAccueil.add(pnlInfo, gridBagConstraints);

        pnlAction.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlAction.setMaximumSize(new java.awt.Dimension(600, 35));
        pnlAction.setMinimumSize(new java.awt.Dimension(600, 35));
        pnlAction.setPreferredSize(new java.awt.Dimension(600, 35));

        btnSuivant.setMnemonic('S');
        btnSuivant.setText("Suivant");
        btnSuivant.setEnabled(false);
        btnSuivant.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuivantActionPerformed(evt);
            }
        });
        pnlAction.add(btnSuivant);

        btnFermer.setMnemonic('F');
        btnFermer.setText("Fermer");
        pnlAction.add(btnFermer);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        pnlAccueil.add(pnlAction, gridBagConstraints);

        getContentPane().add(pnlAccueil, "card2");

        pnlInformation.setMaximumSize(new java.awt.Dimension(700, 225));
        pnlInformation.setMinimumSize(new java.awt.Dimension(650, 170));
        pnlInformation.setPreferredSize(new java.awt.Dimension(650, 180));
        pnlInformation.setLayout(new java.awt.GridBagLayout());

        pnlRapport.setMaximumSize(new java.awt.Dimension(600, 300));
        pnlRapport.setMinimumSize(new java.awt.Dimension(600, 300));
        pnlRapport.setPreferredSize(new java.awt.Dimension(600, 200));
        pnlRapport.setLayout(new java.awt.BorderLayout());

        txaAffichage.setColumns(20);
        txaAffichage.setEditable(false);
        txaAffichage.setFont(new java.awt.Font("Courier New", 0, 12));
        txaAffichage.setRows(5);
        jScrollPane1.setViewportView(txaAffichage);

        pnlRapport.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.ipady = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        pnlInformation.add(pnlRapport, gridBagConstraints);

        pnlAction1.setMaximumSize(new java.awt.Dimension(600, 30));
        pnlAction1.setMinimumSize(new java.awt.Dimension(600, 30));
        pnlAction1.setPreferredSize(new java.awt.Dimension(600, 30));

        btnPrecedent.setMnemonic('P');
        btnPrecedent.setText("Précedent");
        btnPrecedent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrecedentActionPerformed(evt);
            }
        });
        pnlAction1.add(btnPrecedent);

        btnValider.setMnemonic('V');
        btnValider.setText("Valider");
        btnValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValiderActionPerformed(evt);
            }
        });
        pnlAction1.add(btnValider);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlInformation.add(pnlAction1, gridBagConstraints);

        getContentPane().add(pnlInformation, "card3");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void creeComposant()
    {
     JTextFieldMaxLength laMatricule = new JTextFieldMaxLength(6);
     laMatricule.setName ("txaMatriculeEdt");
     laMatricule.addFocusListener(new java.awt.event.FocusAdapter() {
    public void focusLost(java.awt.event.FocusEvent evt) {
        ValiderInformation(evt);
    }
    });
    pnlInfo.add(laMatricule);
    }

    private void chargerInformation()
    {
    IniFile.lectureFichier ();
    String lsAdresse = IniFile.getValeurSection (0);
    String lsTablename = IniFile.getValeurSection (2);
    if (IniFile.getSize () > 0)
        {
//        gaConnect = new SQL_Connection(lsTablename, lsAdresse, "Info", "Info");
        gaEtudiant = new SQL_Etudiant(lsTablename, lsAdresse, "Info", "Info");
        cbxProgramme = gaEtudiant.chargeListeProgramme (cbxProgramme);
        }
    else
    {
        JOptionPane.showMessageDialog (this, "Aucune connection n'a été initialiser.");
    }
    }

    private void btnSuivantActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSuivantActionPerformed
    {//GEN-HEADEREND:event_btnSuivantActionPerformed
    ((CardLayout)getContentPane().getLayout()).next(getContentPane());
    String lsRapport;
    Encodage laEncode = new Encodage(txfMotPasse.getText().trim(),0);
    String lsMotPasse = laEncode.StringToEnc ();

    lsRapport = ("******* Information sur l'étudiant à ajouter *******\n\n" +
                " Nom de l'étudiant    : " + txfNom.getText ().trim () + "\n" +
                " Prenom de l'étudiant : " + txfPrenom.getText ().trim () + "\n" +
                " Matricule            : " + txfMatricule.getText().trim () + "\n" +
                " Mot de passe         : " + lsMotPasse + "\n" +
                " Programme            : " + cbxProgramme.getItemAt (cbxProgramme.getSelectedIndex ()));
    txaAffichage.setText (lsRapport);
    }//GEN-LAST:event_btnSuivantActionPerformed

    private void btnPrecedentActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPrecedentActionPerformed
    {//GEN-HEADEREND:event_btnPrecedentActionPerformed
     ((CardLayout)getContentPane().getLayout()).first (getContentPane());
    }//GEN-LAST:event_btnPrecedentActionPerformed

    private void btnValiderActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnValiderActionPerformed
    {//GEN-HEADEREND:event_btnValiderActionPerformed
    Encodage laEncode = new Encodage(txfMotPasse.getText().trim(),0);
    String lsMotPasse = laEncode.StringToEnc ();
    if (gaEtudiant.ajouterUtilisateur (txfPrenom.getText (),txfNom.getText (),
                                  txfMatricule.getText (), lsMotPasse,
                                  cbxProgramme.getSelectedIndex ()+1))
    {
    JOptionPane.showMessageDialog (this, "L'ajout s'est effectué avec succès.");
    setVisible(false);
    }
    else
    {
    JOptionPane.showMessageDialog (this, "Erreur ! Veuillez vérifier les informations de connection.");
    }
    }//GEN-LAST:event_btnValiderActionPerformed

    private void ValiderInformation (java.awt.event.FocusEvent evt)//GEN-FIRST:event_ValiderInformation
    {//GEN-HEADEREND:event_ValiderInformation
    if (evt.getComponent ().getName ().equals ("Nom"))
    {
        if (!txfNom.getText ().isEmpty ())
        {
            if (!ValiderNom(txfNom.getText ().trim ()))
            {
            JOptionPane.showMessageDialog (this, "Nom invalide.");
            txfNom.setText (null);
            txfNom.requestFocus ();
            }
        }
        else
        {
            txfNom.requestFocus ();
        }
    }
    else if ((evt.getComponent ().getName ().equals ("Prenom")) & (!txfNom.getText ().isEmpty ()))
    {
        if (!txfPrenom.getText ().isEmpty ())
        {
            if (!ValiderNom(txfPrenom.getText ().trim ()))
            {
            JOptionPane.showMessageDialog (this, "Prénom invalide.");
            txfPrenom.setText (null);
            txfPrenom.requestFocus ();
            }
        }
        else
        {
            txfPrenom.requestFocus ();
        }
    }
    else if ((evt.getComponent ().getName ().equals ("Matricule")) & (!txfPrenom.getText ().isEmpty ()))
    {
        if (!txfMatricule.getText ().isEmpty ())
        {
            if (!ValiderMatricule(txfMatricule.getText ().trim ()))
            {
                JOptionPane.showMessageDialog (this, "Matricule Invalide");
                txfMatricule.setText (null);
                txfMatricule.requestFocus ();
            }
        }
        else
        {
            txfMatricule.requestFocus ();
        }
    }
    else if ((evt.getComponent ().getName ().equals ("MotPasse")) & (!txfMatricule.getText ().isEmpty ()))
    {
        if (!txfMotPasse.getText ().isEmpty ())
        {
            if (!ValiderMotPasse(txfMotPasse.getText ().trim ()))
            {
                JOptionPane.showMessageDialog (this, "Mot de passe invalide");
                txfMotPasse.setText (null);
                txfMotPasse.requestFocus ();
            }
        }
        else
        {
            txfMotPasse.requestFocus ();
        }
    }
    else if ((evt.getComponent ().getName ().equals ("Programme")) & (!txfMotPasse.getText ().isEmpty ()))
    {
        if (cbxProgramme.getSelectedIndex () == -1)
        {
        JOptionPane.showMessageDialog (this, "Veuillez sélectionner un programme");
        txfMotPasse.requestFocus ();
        }
        else
        {
        btnSuivant.setEnabled (true);
        }

    }
    }//GEN-LAST:event_ValiderInformation

    public boolean ValiderNom(String psTexte)
    {
    return psTexte.matches ("\\p{Upper}(\\p{Lower}+\\s?){2,20}");
    }

    public boolean ValiderMatricule(String psMatricule)
    {
    return psMatricule.matches ("[0-9]{6}");
    }

    public boolean ValiderMotPasse(String psMotPasse)
    {
    return psMotPasse.matches ("^[A-Z](.{2,11})$");
    // [A-Z] + [0-9]ou[a-z] = 3 a 15 carac
    // "\\p{Upper}(.{Lower}+\\s?){3,15}"
    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmInscription().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFermer;
    private javax.swing.JButton btnPrecedent;
    private javax.swing.JButton btnSuivant;
    private javax.swing.JButton btnValider;
    private javax.swing.JComboBox cbxProgramme;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlAccueil;
    private javax.swing.JPanel pnlAction;
    private javax.swing.JPanel pnlAction1;
    private javax.swing.JPanel pnlInfo;
    private javax.swing.JPanel pnlInformation;
    private javax.swing.JPanel pnlRapport;
    private javax.swing.JTextArea txaAffichage;
    private javax.swing.JTextField txfMatricule;
    private javax.swing.JPasswordField txfMotPasse;
    private javax.swing.JTextField txfNom;
    private javax.swing.JTextField txfPrenom;
    // End of variables declaration//GEN-END:variables
    private void positionnerFenetre ()
    {
        Dimension laTailleEcran =
                          Toolkit.getDefaultToolkit ().getScreenSize ();
        Dimension laTailleFenetre = this.getSize ();
        if (laTailleFenetre.height > laTailleEcran.height)
        {
            laTailleFenetre.height = laTailleEcran.height;
        }
        if (laTailleFenetre.width > laTailleEcran.width)
        {
            laTailleFenetre.width = laTailleEcran.width;
        }
        this.setLocation((laTailleEcran.width - laTailleFenetre.width) / 2,
                        (laTailleEcran.height - laTailleFenetre.height) / 2);
    }

}
